package com.example.consumer.io;

import ch.ethz.ssh2.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;

public class ReadLinuxFile {

    private final static Logger LOGGER = LoggerFactory.getLogger(ReadLinuxFile.class);

    public static void main(String[] args) {
        try {
            Connection connection = new Connection("192.168.0.105");
            connection.connect();
            boolean isAuth = connection.authenticateWithPassword("root", "zz0629");
            if (!isAuth) {
                throw new IOException("fail");
            }
            Session session = connection.openSession();
            session.execCommand("df -h");
            InputStream input = new StreamGobbler(session.getStdout());
            BufferedReader buffer = new BufferedReader(new InputStreamReader(input));
            String line = null;
            while( (line=buffer.readLine()) != null ) {
                System.out.println(line);
            }
            buffer.close();

            FileOutputStream outputStream = new FileOutputStream("D:\\workspace\\test\\ooxx.txt");
            SCPClient client = connection.createSCPClient();
            SCPInputStream scpInputStream = client.get("/fish/ooxx.txt");
            byte[] bytes = new byte[1024];
            int len = 0;
            while ( (len=scpInputStream.read(bytes)) != -1){
                outputStream.write(bytes);
            }
            outputStream.close();

            SFTPv3Client sftPv3Client = new SFTPv3Client(connection);
            sftPv3Client.createFile("/fish/ooxx2.txt");
            sftPv3Client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
